package net.lcqf.tcp.send.message;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import io.netty.channel.Channel;
import net.lcqf.tcp.constants.Constants;
import net.lcqf.tcp.game.MatchRoom;
import net.lcqf.tcp.game.match.model.MatchPlayer;
import net.lcqf.tcp.game.room.RoomManager;
import net.lcqf.tcp.request.HeroRequest;
import net.lcqf.tcp.response.BattleHeroResponse;
import net.lcqf.tcp.response.HeroResponse;
import net.lcqf.tcp.send.MessageBaseService;
import net.lcqf.tcp.send.MessageFactory;
import net.lcqf.tcp.send.RequestType;
import net.lcqf.tcp.server.MsgInfoPojo;
import net.lcqf.tcp.utils.LocalCacheUtil;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * +----------------------------------------------------------------------
 * // | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
 * // +----------------------------------------------------------------------
 * // | Copyright (c) 2021-2025 聊来 All rights reserved.
 * // +----------------------------------------------------------------------
 * // | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">apache</a> )
 * // +----------------------------------------------------------------------
 * // | @Author: 聊来 <18970881148@qq.com>
 * // +----------------------------------------------------------------------
 * // | DateTime: 2025/3/26 18:42
 * // +----------------------------------------------------------------------
 */

@Service
public class Hero implements MessageBaseService {

    @Override
    @PostConstruct
    public void init() {
        MessageFactory.register(RequestType.SELECT_HERO, this);
        MessageFactory.register(RequestType.LOAD_HERO_PROGRESS, this);
    }


    /**
     * TODO 选择英雄，查看英雄详情
     * @param channel 消息通道
     * @param msg 内容
     * @return {@code Object}
     */
    @Override
    public Object sendMessage(Channel channel, String msg) {
        HeroRequest request = JSONUtil.toBean(msg, HeroRequest.class);


        checkPlayerBehavior(request, channel);

        return null;
    }


    /**
     * 检测玩家行为
     * @param request 请求参数
     * @param channel 消息通道
     */
    private void checkPlayerBehavior(HeroRequest request, Channel channel) {
        RoomManager roomClass = LocalCacheUtil.ACTIVE_ROOM.get(request.getRoomId());
        if (ObjectUtil.isNull(roomClass)) { return; }
        String channelId = channel.id().asShortText();
        switch (request.getType()) {
            case 7:

                boolean result = roomClass.confirmHero(channelId, request.getHeroId());
                if (!result) { return; }

                HeroResponse hero = new HeroResponse();
                hero.setMapId(101);

                List<BattleHeroResponse> battleHeroData = new ArrayList<>();
                for (Map.Entry<String, MatchPlayer> player : roomClass.players.entrySet()) {
                    BattleHeroResponse battleHero = new BattleHeroResponse();
                    battleHero.setHeroId(player.getValue().getHeroId());
                    battleHero.setChannelId(player.getKey());
                    battleHero.setUserName(player.getValue().getUserName());
                    battleHero.setPercent(Constants.NUM_ZERO);
                    battleHeroData.add(battleHero);
                }
                hero.setBattleHero(battleHeroData);


                MsgInfoPojo.MsgInfo response = MsgInfoPojo.MsgInfo.newBuilder()
                        .setRequestType(RequestType.SELECT_HERO.ordinal())
                        .setData(JSONUtil.toJsonStr(hero))
                        .build();

                for (int i = 0; i < roomClass.matchPlayerList.size(); i++) {
                    MatchPlayer player = roomClass.matchPlayerList.get(i);
                    if (player.getChannel().isActive()) { player.getChannel().writeAndFlush(response); }
                }


                //MatchRoom.broadcastToPlayer(RequestType.SELECT_HERO.ordinal(), JSONUtil.toJsonStr(hero), roomClass.matchPlayerList);
                break;
            case 8:
                List<Integer> percentList = new ArrayList<>();
                for (int i = 0; i < roomClass.matchPlayerList.size(); i++) {
//                    if (player.getKey().equals(channel.id().asShortText())) {
//                        percentList.add(request.getPercent());
//                    } else {
//                        percentList.add(Constants.NUM_ZERO);
//                    }
                    percentList.add(request.getPercent());
                }
                MatchRoom.broadcastToPlayer(RequestType.LOAD_HERO_PROGRESS.ordinal(), JSONUtil.toJsonStr(percentList), roomClass.matchPlayerList);
                break;
            default:
                if (channel.isActive()) { channel.close(); }
        }
    }
}
